package com.gdth.bank.activity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import com.alibaba.fastjson.JSONObject;
import com.gdth.bank.BankApplication;
import com.gdth.bank.R;
import com.gdth.bank.event.CjkhjlListEvent;
import com.gdth.bank.presenter.ChangePasswordPresenter;
import com.gdth.bank.view.ChangePasswordView;
import com.gdth.core.annotation.view.ViewInject;
import com.gdth.core.widget.BaseActivity;
import com.gdth.database.bean.TAppYh;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

// 修改密码
public class ChangePasswordActivity extends BaseActivity implements ChangePasswordView{
	@ViewInject(id = R.id.header_label_title)
	TextView txt_header_title;
	@ViewInject(id = R.id.header_btn_left, click = "finishClick")
	TextView txt_header_left;
	@ViewInject(id = R.id.header_btn_right)
	TextView txt_header_right;
	@ViewInject(id = R.id.user_zh)
	TextView userZh;
	@ViewInject(id = R.id.user_old_password)
	EditText oldPassword;
	@ViewInject(id = R.id.user_new_password)
	EditText newPassword;
	@ViewInject(id = R.id.user_new_password1)
	EditText newPassword1;
	@ViewInject(id = R.id.btnOk)
	Button btnOk; 
	private ChangePasswordPresenter presenter;
	
	String NameString = "";
	String oldPWString = "";
	String newPwString = "";
	String newPwString1 = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_password);
		EventBus.getDefault().register(this);
		presenter = new ChangePasswordPresenter(this);
		TAppYh user = BankApplication.mInstance.getCurrentUser();
		txt_header_title.setText(getResources().getString(R.string.param100));
	    txt_header_right.setVisibility(View.GONE);
	    NameString = user.getZh();
	    userZh.setText(NameString);
	    btnOk.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onUpload();
			}
		});
	    
 	}
	
	public  void onUpload() {
		NameString = userZh.getText().toString();
		oldPWString = oldPassword.getText().toString();
		newPwString = newPassword.getText().toString();
		newPwString1 = newPassword1.getText().toString();
		if(NameString.isEmpty()) {
			ShowTishi(getResources().getString(R.string.param101));
			return;
		}
		if(oldPWString.isEmpty()) {
			ShowTishi(getResources().getString(R.string.param102));
			return;
		}
		if(newPwString.isEmpty()) {
			ShowTishi(getResources().getString(R.string.param103));
			return;
		}
		if(newPwString1.isEmpty()) {
			ShowTishi(getResources().getString(R.string.param104));
			return;
		}
		if(!newPwString.equals(newPwString1)) {
			ShowTishi(getResources().getString(R.string.param105));
			return;
		}
		
		String regStr = "^(?![0-9A-Z]+$)(?![0-9a-z]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$";
		if(!newPwString.matches(regStr)) { 		//校验密码
			ShowTishi(getResources().getString(R.string.param106));
			return ;
		}
		JSONObject param = new JSONObject();
		param.put("zh", NameString);
		param.put("password", oldPWString);
		param.put("newpassword", newPwString);
		presenter.changepassword(param.toString());
		
		
	}
	
	public void ShowTishi(String msg) {
		Toast.makeText(ChangePasswordActivity.this, msg, Toast.LENGTH_SHORT).show();
	}

	@Override
	public void showProgressDialog(String msg) {
		// TODO Auto-generated method stub
//		mDialogLoader.setMessage(msg);
//    	mDialogLoader.show();
	}

	@Override
	public void hideProgressDialog() {
		// TODO Auto-generated method stub
//		mDialogLoader.hide();
	}

	@Override
	public void showMessage(String msg) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void changepassword(String str) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void success() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void falseCallback() {
		// TODO Auto-generated method stub
		
	}
	private AlertDialog.Builder builder;
	@Subscribe(threadMode = ThreadMode.MAIN)
	public void getList(CjkhjlListEvent Event) {
		//query(1);
		if(Event.getType() == CjkhjlListEvent.CHANGE_PASSWORD) {		// 修改密码返回
			JSONObject jsonObject = JSONObject.parseObject(Event.getUuid()); // uuid 是后台返回的code 和msg
			int code = jsonObject.getInteger("code");
			if(code == 200) {
				showDialog(getResources().getString(R.string.param107));
			}else {
				ShowTishi(jsonObject.getString("msg"));
			}
			
		}
	}
	
	  private void showDialog(String content) {

	        builder = new AlertDialog.Builder(this).setTitle("提示 ").setCancelable(false)
	                .setMessage(content).setNeutralButton("确定",new DialogInterface.OnClickListener() {
	                    @Override
	                    public void onClick(DialogInterface dialogInterface, int i) {
	                        //ToDo: 你想做的事情
	                    	 dialogInterface.dismiss();
	                    	 startActivity(new Intent(ChangePasswordActivity.this, LoginActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
	                    }
	                });
	        builder.create().show();
	    }
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		EventBus.getDefault().unregister(this);
	}

}
